package org.eclipse.soc.ide4edu.assignment.wizards;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.soc.ide4edu.assignment.operations.LoginMap;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

public class UploadWizardPage1 extends WizardPage {

	ISelection select;
	private Text url;
	private Text containerField;
	private Combo serverName;
	private URL finalURL = null;

	public UploadWizardPage1(ISelection selection) {
		super("UploadWizardPage1");
		setTitle("Server Info");
		this.select = selection;

	}

	private void addNames(Combo serverName) {
		final String[] names = { "Moodle" };
		serverName.setItems(names);
	}

	private void checkURL() {
		try {
			new URL(url.getText());
		} catch (MalformedURLException e) {
			updateStatus("Invalid URL");
			return;
		}
		updateStatus(null);
	}

	public void createControl(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout();
		composite.setLayout(gl);

		createGeneralDetailsSection(composite);
		composite.layout();
		setControl(composite);

	}

	private void createGeneralDetailsSection(Composite composite) {

		GridLayout gl = new GridLayout();
		gl.numColumns = 3;
		GridData gd = new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL);

		Group general = new Group(composite, SWT.NONE);
		general.setLayout(gl);

		new Label(general, SWT.NONE).setText("Server Address: ");
		url = new Text(general, SWT.BORDER | SWT.SINGLE);
		url.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				checkURL();
			}
		});
		url.setLayoutData(gd);

		gd.horizontalSpan = 2;

		new Label(general, SWT.NONE).setText("Server Name: ");
		serverName = new Combo(general, SWT.READ_ONLY | SWT.DROP_DOWN);

		addNames(serverName);

		serverName.setLayoutData(gd);
		gd.horizontalSpan = 2;

		new Label(general, SWT.NONE).setText("Select Assignment: ");
		containerField = new Text(general, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		containerField.setLayoutData(gd);
		containerField.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		Button browse = new Button(general, SWT.PUSH);
		browse.setText("Browse...");
		browse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		general.setLayoutData(gd);
		general.setText("General Info");

	}

	private void dialogChanged() {
		IResource container = ResourcesPlugin.getWorkspace().getRoot()
				.findMember(new Path(getContainerName()));

		if (getContainerName().length() == 0) {
			updateStatus("Assignment must be specified");
			return;
		}

		if (container == null
				|| (container.getType() & (IResource.PROJECT)) == 0) {
			updateStatus("Assignment must exist in the Workspace");
			return;
		}
		updateStatus(null);
	}

	public String getAssignmentName() {
		String name = getAssignmentResource().getName();
		return name;
	}

	public IResource getAssignmentResource() {
		IResource assignment = ResourcesPlugin.getWorkspace().getRoot()
				.findMember(new Path(getContainerName()));

		return assignment;
	}

	public String getContainerName() {
		return containerField.getText();
	}

	public URL getFinalURL() {

		LoginMap lMap = new LoginMap();
		finalURL = lMap.getLoginUrl(getServerURL(), getServerName());
		return finalURL;

	}

	public String getServerName() {
		return serverName.getText();
	}

	public String getServerURL() {
		return url.getText();
	}

	private void handleBrowse() {
		ContainerSelectionDialog dialog = new ContainerSelectionDialog(
				getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
				"Select the Assignment to be Uploaded");

		if (dialog.open() == Window.OK) {
			Object[] result = dialog.getResult();
			if (result.length == 1) {
				containerField.setText(((Path) result[0]).toString());
			}
		}
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

}
